package api.json.demo003.config;

import javax.servlet.http.HttpSession;

import apijson.NotNull;
import com.alibaba.fastjson.JSONObject;

import apijson.RequestMethod;
import apijson.framework.APIJSONFunctionParser;

/**
 * 可远程调用的函数类，用于自定义业务逻辑处理
 * 具体见 https://github.com/Tencent/APIJSON/issues/101
 */
public class MySQLFunctionParser extends APIJSONFunctionParser {

    public static final String TAG = "MySQLFunctionParser";

    public MySQLFunctionParser() {
        this(null, null, 0, null, null);
    }

    public MySQLFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) {
        super(method, tag, version, request, session);
    }


    /**
     * {
     *  "name": "jerry",
     *  "ref()": "sayHello(name)"
     * }
     */
    /* INSERT INTO `function` VALUES (7, 0, 0, NULL, 'sayHello', 'Object', 'name', '{\"name\": \"jerry\"}', '测试方法', 0, NULL, NULL, '2024-05-21 09:12:23', NULL); */
    public Object sayHello(@NotNull JSONObject current, @NotNull String name) throws Exception {
        // 注意这里参数 name 是 key，不是 value
        Object obj = current.get(name);
        if (obj == null) {
            return "参数不能为空";
        }
        if (!(obj instanceof String)) {
            return "参数不合法";
        }
        return "Hello, " + obj.toString();
    }
}